package com.example.hzzrpcclient.config;//package com.example.hzzrpcclient.config;
//
//
//import com.example.hzzrpcclient.config.annotations.EnableRpcClient;
//import com.example.hzzrpcclient.config.annotations.RpcAutowired;
//import com.example.hzzrpcclient.config.netty.NettyMainClient;
//import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
//import org.springframework.beans.factory.config.BeanDefinition;
//import org.springframework.beans.factory.support.BeanDefinitionRegistry;
//import org.springframework.boot.autoconfigure.AutoConfigureAfter;
//import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
//import org.springframework.context.EnvironmentAware;
//import org.springframework.context.ResourceLoaderAware;
//import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Import;
//import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
//import org.springframework.core.env.Environment;
//import org.springframework.core.io.ResourceLoader;
//import org.springframework.core.type.AnnotationMetadata;
//import org.springframework.core.type.filter.AnnotationTypeFilter;
//import org.springframework.util.Assert;
//import org.springframework.util.ClassUtils;
//import org.springframework.util.StringUtils;
//
//import java.util.HashSet;
//import java.util.LinkedHashSet;
//import java.util.Map;
//import java.util.Set;
//
///**
// * @author ：hzz
// * @description：TODO
// * @date ：2022/1/2 11:05
// */
//@AutoConfigureAfter(RedisAutoConfiguration.class)
//@Import(NettyMainClient.class)
//@Configuration
//public class RpcServerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware {
//    private ResourceLoader resourceLoader;
//    private Environment environment;
//
//    @Override
//    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
//        registerRpcServer(importingClassMetadata, registry);
//    }
//
//    /**
//     * @param metadata 这个其实就是这个注解所在的类。这里就是启动类
//     * @param registry
//     */
//    public void registerRpcServer(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
//        LinkedHashSet<BeanDefinition> candidateComponents = new LinkedHashSet<>();
//
//        Set<String> basePackages = getBasePackages(metadata);
//        ClassPathScanningCandidateComponentProvider scanner = getScanner();
//        scanner.setResourceLoader(this.resourceLoader);
//        scanner.addIncludeFilter(new AnnotationTypeFilter(RpcAutowired.class));
//        for (String basePackage : basePackages) {
//            candidateComponents.addAll(scanner.findCandidateComponents(basePackage));
//        }
//
//        /**
//         * 扫描所有找到的对应服务提供者实现类的bean
//         */
//        for (BeanDefinition candidateComponent : candidateComponents) {
//            if (candidateComponent instanceof AnnotatedBeanDefinition) {
//                AnnotationMetadata annotationMetadata = ((AnnotatedBeanDefinition) candidateComponent).getMetadata();
//                Assert.isTrue(annotationMetadata.isInterface(), "@RpcAutowired can only be specified on an interface");
//                Map<String, Object> attributes = annotationMetadata
//                        .getAnnotationAttributes(RpcAutowired.class.getCanonicalName());
//                String serverName = String.valueOf(attributes.get("ServerName"));
//                if(serverName==null||"".equalsIgnoreCase(serverName)){
//                    serverName= candidateComponent.getBeanClassName();
//                }
//                System.out.println(serverName);
//            }
//        }
//    }
//
//    /**
//     * 找到所有要扫描注册的服务发布者的处理类
//     *
//     * @param importingClassMetadata
//     * @return
//     */
//    protected Set<String> getBasePackages(AnnotationMetadata importingClassMetadata) {
//        Map<String, Object> attributes = importingClassMetadata
//                .getAnnotationAttributes(EnableRpcClient.class.getCanonicalName());
//
//        Set<String> basePackages = new HashSet<>();
//
//        for (String pkg : (String[]) attributes.get("basePackages")) {
//            if (StringUtils.hasText(pkg)) {
//                basePackages.add(pkg);
//            }
//        }
//
//        if (basePackages.isEmpty()) {
//            basePackages.add(ClassUtils.getPackageName(importingClassMetadata.getClassName()));
//        }
//        return basePackages;
//    }
//
//
//    protected ClassPathScanningCandidateComponentProvider getScanner() {
//        return new ClassPathScanningCandidateComponentProvider(false, this.environment) {
//            @Override
//            protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
//                boolean isCandidate = false;
//                if (beanDefinition.getMetadata().isIndependent()) {
//                    if (!beanDefinition.getMetadata().isAnnotation()) {
//                        isCandidate = true;
//                    }
//                }
//                return isCandidate;
//            }
//        };
//    }
//
//    @Override
//    public void setEnvironment(Environment environment) {
//        this.environment = environment;
//    }
//
//    @Override
//    public void setResourceLoader(ResourceLoader resourceLoader) {
//        this.resourceLoader = resourceLoader;
//    }
//}
